import { request } from "@/http/axios"
import type { 
  DeliveryRecordItem,
  DeliveryAreaItem,
  GetDeliveryRecordsRequestData, 
  GetDeliveryRecordsResponseData, 
  CreateDeliveryRecordRequestData, 
  UpdateDeliveryRecordRequestData,
  DeliveryStatsData,
  GetDeliveryAreasRequestData,
  GetDeliveryAreasResponseData,
  CreateDeliveryAreaRequestData,
  UpdateDeliveryAreaRequestData
} from "./type"

/** 获取配送记录列表 */
export function getDeliveryRecordsApi(params?: GetDeliveryRecordsRequestData) {
  return request<GetDeliveryRecordsResponseData>({
    url: "delivery/records",
    method: "get",
    params
  })
}

/** 获取配送记录详情 */
export function getDeliveryRecordApi(id: string) {
  return request<DeliveryRecordItem>({
    url: `delivery/records/${id}`,
    method: "get"
  })
}

/** 创建配送记录 */
export function createDeliveryRecordApi(data: CreateDeliveryRecordRequestData) {
  return request<DeliveryRecordItem>({
    url: "delivery/records",
    method: "post",
    data
  })
}

/** 更新配送记录 */
export function updateDeliveryRecordApi(id: string, data: UpdateDeliveryRecordRequestData) {
  return request<DeliveryRecordItem>({
    url: `delivery/records/${id}`,
    method: "put",
    data
  })
}

/** 删除配送记录 */
export function deleteDeliveryRecordApi(id: string) {
  return request({
    url: `delivery/records/${id}`,
    method: "delete"
  })
}

/** 获取配送统计数据 */
export function getDeliveryStatsApi(startDate?: string, endDate?: string) {
  return request<DeliveryStatsData>({
    url: "delivery/stats",
    method: "get",
    params: { startDate, endDate }
  })
}

/** 获取配送区域列表 */
export function getDeliveryAreasApi(params?: GetDeliveryAreasRequestData) {
  return request<GetDeliveryAreasResponseData>({
    url: "delivery/areas",
    method: "get",
    params
  })
}

/** 获取配送区域详情 */
export function getDeliveryAreaApi(id: string) {
  return request<DeliveryAreaItem>({
    url: `delivery/areas/${id}`,
    method: "get"
  })
}

/** 创建配送区域 */
export function createDeliveryAreaApi(data: CreateDeliveryAreaRequestData) {
  return request<DeliveryAreaItem>({
    url: "delivery/areas",
    method: "post",
    data
  })
}

/** 更新配送区域 */
export function updateDeliveryAreaApi(id: string, data: UpdateDeliveryAreaRequestData) {
  return request<DeliveryAreaItem>({
    url: `delivery/areas/${id}`,
    method: "put",
    data
  })
}

/** 删除配送区域 */
export function deleteDeliveryAreaApi(id: string) {
  return request({
    url: `delivery/areas/${id}`,
    method: "delete"
  })
}

/** 获取区域排行榜 */
export function getAreaRankingApi(type: 'easy' | 'hard' = 'easy', limit: number = 10) {
  return request<DeliveryAreaItem[]>({
    url: "delivery/areas/ranking",
    method: "get",
    params: { type, limit }
  })
}